package HighLevelFunction

import javax.xml.crypto.dsig.keyinfo.KeyValue

object ComplexWordCount {
  def main(args: Array[String]): Unit = {
    val stringList: List[(String,Int)] = List(
      ("hello",1),
      ("hello world",4),
      ("hello scala",3),
      ("hello spark",2),
      ("hello spark from scala",1),
      ("hello flink from scala",2)
    )
    val newStringList:List[String]=stringList.map(
      kv=>{
        (kv._1.trim+" ")*kv._2
      }
    )
    println(newStringList)
    //打散
    val stringListOne:List[String]=newStringList.flatMap(_.split(" "))
    println(stringListOne)
    //分组
    val groupList:Map[String,List[String]]=stringListOne.groupBy(
      kv=>kv
    )
    println(groupList)
    //统计
    val listCount: Map[String, Int] = groupList.map(
      kv => {
        val key = kv._1
        val count = kv._2.length
        (key, count)
      }
    )
    println(listCount)
    //取前三(map转换为list)
    val tuples: List[(String, Int)] = listCount.toList.sortWith(
      _._2>_._2
    )
    println(tuples.take(3))
    //或者合并写法
    println("==============================")
    val tuples1: List[(String, Int)] = newStringList.flatMap(_.split(" "))
      .groupBy(
        word => word
      ).map(kv => (kv._1, kv._2.size))
      .toList
      .sortBy(_._2)
      .reverse
      .take(3)
    println(tuples1)
  }

}
